package cn.b.sky.flowable.configuration;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.common.api.delegate.event.FlowableEventListener;
import org.flowable.engine.common.impl.history.HistoryLevel;
import org.flowable.spring.SpringAsyncExecutor;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class WorkflowConfiguration extends AbstractProcessEngineAutoConfiguration {

    @Bean
    @Override
    protected SpringProcessEngineConfiguration baseSpringProcessEngineConfiguration(DataSource dataSource,
                                                                                    PlatformTransactionManager platformTransactionManager, SpringAsyncExecutor springAsyncExecutor)
            throws IOException {
        SpringProcessEngineConfiguration springProcessEngineConfiguration =
                super.baseSpringProcessEngineConfiguration(dataSource, platformTransactionManager, springAsyncExecutor);


        // 设置Event listener
        List<FlowableEventListener>  eventListeners = new ArrayList<>();
        springProcessEngineConfiguration.setEventListeners(eventListeners);
        // 禁用Activiti默认用户权限
        springProcessEngineConfiguration.setDisableIdmEngine(true);
        springProcessEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 不保留历史记录
        springProcessEngineConfiguration.setDbHistoryUsed(false);
        springProcessEngineConfiguration.setHistoryLevel(HistoryLevel.NONE);
        return springProcessEngineConfiguration;
    }

}
